﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace SimpleTerminal.Controls
{
    public partial class TabLabelControl : Control
    {
        private bool selected = false;
        private string text = "";

        //private Color backgroundColor = Color.FromArgb(40, 43, 61);
        private Color textColor = Color.Black;
        //private Color selectdBackgroundColor = Color.FromArgb(20, 23, 41);
        private Color selectedTextColor = Color.Black;
        //private Color selectedTextColor = Color.White;
        protected Color hoverColor = Color.FromArgb(218, 218, 218);
        //public new EventHandler Click;
        private Rectangle rectangle = new Rectangle();
        protected bool hover = false;
        public string TAG = "TabLabelControl";

        private bool indicatorActive = false;


        public EventHandler Closed;
        public EventHandler<MoveLabelEventArgs> MoveLablel;
        public EventHandler ToOther;


        public class MoveLabelEventArgs : EventArgs
        {
            int offset;
            public int Offset { get => offset; set => offset = value; }
        }
        SizeF sizeF;
        public TabLabelControl()
        {

            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            InitializeComponent();
           
            tabLabelCloseItem.Click += Close_Click;
            tabLabelToOther.Click += Tab_ToOther;
        }

        private void Tab_ToOther(object sender, EventArgs e)
        {
            ToOther?.Invoke(this, EventArgs.Empty);
        }
        private void Close_Click(object sender, EventArgs e)
        {
            Closed?.Invoke(this, EventArgs.Empty);
        }


        public void DrawIndicator(Graphics g)
        {
            /*
             * 绘制两种状态的指示器
             * 分别代表链接是否激活
             */
            Color color = IndicatorActive ?
                Color.FromArgb(64, 131, 201) : Color.FromArgb(204, 204, 204);
            using (SolidBrush solidBrush = new SolidBrush(color))
            {
                g.FillEllipse(solidBrush, 10, Height / 2 - 3 + 1, 6, 6);
            }
           // g.DrawImage(Properties.Resources.ICON_11, 5, (Height - 17) / 2 + 1, 17, 17);
        }

        Rectangle closeRectangle;


        public GraphicsPath CreateBounds()
        {
            int radius = 5;
            int diameter = radius * 2;

            Rectangle rectangle = new Rectangle(0,0,Width-1,Height);

            GraphicsPath graphicsPath = new GraphicsPath();
            graphicsPath.StartFigure();
            graphicsPath.AddArc(rectangle.Left, rectangle.Top, diameter,diameter,170,90);
            graphicsPath.AddLine(radius, rectangle.Top, rectangle.Width - radius, rectangle.Top);
            graphicsPath.AddArc(rectangle.Width - diameter, rectangle.Top, diameter, diameter, 270, 90);
            graphicsPath.AddLine(rectangle.Width, radius, rectangle.Width, rectangle.Height);
            graphicsPath.AddLine(rectangle.Width, rectangle.Height, rectangle.Left, rectangle.Height);
            graphicsPath.AddLine(rectangle.Left, rectangle.Height,rectangle.Left, rectangle.Top + radius);

            graphicsPath.CloseFigure();
            return graphicsPath;
        }

        public void DoPaint(Graphics graphics)
        {
            //graphics.PageUnit = GraphicsUnit.Pixel;
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            //this.Height = (int)sizeF.Height + Padding.Top + Padding.Bottom;


            Color tc = selected ? selectedTextColor : (hover ? selectedTextColor : textColor);

            //BackColor = Preference.Default.Terminal_BackColor;
            //BackColor = bg;
            //graphics.Clear(BackColor);
            //绘制连接指示的圆点
            //绘制选中标签的底部横线
            //DarwUnderline(graphics);

            //if (hover || selected)
            //{
            GraphicsPath graphicsPath = CreateBounds();
            if (selected)
            {
                graphics.FillPath(Brushes.White,graphicsPath);
                graphics.DrawPath(Pens.DarkGray, graphicsPath);
                //graphics.DrawLine(Pens.DarkGray, 0, 0, Width - 1, 0);
                //graphics.DrawLine(Pens.DarkGray, 0, 0, 0, Height - 1);
                //graphics.DrawLine(Pens.DarkGray, Width - 1, 0, Width - 1, Height - 1);
            }
            else if (hover)
            {
                graphics.FillPath(new SolidBrush(hoverColor), graphicsPath);
                //graphics.Clear(hoverColor);
            }
            else
            {
                graphics.FillPath(new SolidBrush(BackColor), graphicsPath);
            }
            if (hover || selected)
            {
                if (OnCloseHover)
                {
                    using (SolidBrush solidBrush = new SolidBrush(Color.FromArgb(235, 235, 235)))
                    {
                        graphics.FillRectangle(solidBrush, closeRectangle);
                    }
                }
                graphics.DrawImage(Properties.Resources.close, closeRectangle);
            }
            //  }
            DrawIndicator(graphics);

            using (SolidBrush solidBrush1 = new SolidBrush(tc))
            {
                graphics.DrawString(text, Font, solidBrush1,
                     rectangle.X + Padding.Left + 6,
                    (Height - sizeF.Height) / 2 + 1
                     );
            }


        }
        bool OnCloseHover = false;

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            closeRectangle = new Rectangle(Width - 14 - 4, (Height - 14) / 2 + 1, 14, 14);
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (closeRectangle != null)
            {
                if (closeRectangle.Contains(e.X, e.Y))
                {
                    Close_Click(this, EventArgs.Empty);
                    return;
                }
            }
            base.OnMouseDown(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (closeRectangle != null)
            {
                bool hitTest = closeRectangle.Contains(e.X, e.Y);
                if (!OnCloseHover && hitTest)
                {
                    OnCloseHover = true;
                    Invalidate();
                }
                else if (OnCloseHover && !hitTest)
                {
                    OnCloseHover = false;
                    Invalidate();
                }
            }



        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            DoPaint(pe.Graphics);
        }


        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);
            text = Text;
            toolTip.SetToolTip(this, Text);

            Graphics graphics = CreateGraphics();
            sizeF = graphics.MeasureString(Text, Font, Point.Empty, StringFormat.GenericTypographic);
            int w = (int)sizeF.Width + Padding.Left + Padding.Right + 15;
            if (Width != w)
            {
                Width = w;
            }
            graphics.Dispose();

            Invalidate();
        }


        public bool Selected
        {
            set
            {
                selected = value;
                Invalidate();
            }
            get
            {
                return selected;
            }
        }

        public bool IndicatorActive
        {
            get => indicatorActive;
            set
            {
                indicatorActive = value;
                Invalidate();
            }
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            hover = true;
            Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            hover = false;
            OnCloseHover = false;
            Invalidate();
        }
    }
}
